package com.example.vote.utils;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;

import java.awt.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具类
 */
public class MD5Utils {
    public static String getMD5Str(String str) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
        } catch (NoSuchAlgorithmException e) {
            System.out.println("NoSuchAlgorithmException caught!");
            System.exit(-1);
        }

        byte[] byteArray = messageDigest.digest();

        StringBuffer md5StrBuff = new StringBuffer();

        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
            else
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return md5StrBuff.toString();
    }

    public static void main(String[] args) {
        QrConfig config = new QrConfig(300, 300);
        File generate = QrCodeUtil.generate("https://frp-fan.top:57545/", config,
                new File("D:\\development\\uniapp\\vote-uniapp\\vote\\src\\main\\java\\com\\example\\vote\\utils\\下载.png"));
        generate.setReadable(true);
        generate.setWritable(true);
        generate.setExecutable(true);
        System.out.println(generate.getAbsolutePath());
//        String decoded = QrCodeUtil.decode(new File("C:\\Users\\ztf\\Desktop\\vote-uniapp\\vote\\src\\main\\java\\com\\example\\vote\\utils\\下载.png"));
//        System.out.println(decoded);
    }
}
